<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
    public function index(){

    	$sql = "select t1.*,t3.title from admin t1 left join auth_group_access t2 on t1.aid=t2.uid left join auth_group t3 on t2.group_id = t3.id";

    	$data = M()->query($sql);

    	var_dump($data);
        $this->display();
    }

    public function addadmin()
    {	
    	$groups = M('auth_group')->select();
    	$this->assign('groups',$groups);
    	$this->display();
    }

    public function addadmindata()
    {
    	$data = I('post.');

    	$data['password'] = md5($data['password']);

    	$uid = M('admin')->add($data);

    	$groupsData['uid'] = $uid;
		$groupsData['group_id'] = $data['group_id'];

		$res = M('auth_group_access')->add($groupsData);

		if($res){
    		$this->ajaxReturn(['error'=>0,'info'=>"增加成功"]);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>"增加失败"]);
    	}

    }

    public function cate(){

    	$cates = M('auth_cate')->select();
    	$this->assign('cates',$cates);
        $this->display();
    }

    public function addcate()
    {
    	$data = I('post.');

    	$res = M('auth_cate')->add($data);

    	if($res){
    		$this->ajaxReturn(['error'=>0,'info'=>"增加成功"]);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>"增加失败"]);
    	}
    }

    public function rule(){

    	$rules = M('auth_rule t1')->field("t1.*,t2.cname")->join("left join auth_cate t2 on t1.cid=t2.cid ")->select();

    	$cates = M('auth_cate')->select();
    	$this->assign('cates',$cates);
    	$this->assign('rules',$rules);
        $this->display();
    }

     public function addrule()
    {
    	$data = I('post.');

    	$res = M('auth_rule')->add($data);

    	if($res){
    		$this->ajaxReturn(['error'=>0,'info'=>"增加成功"]);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>"增加失败"]);
    	}
    }

    public function role()
    {
    	$groups = M('auth_group')->select();

    	foreach ($groups as $k => $row) {
    		$rules = $row['rules'];

    		$sql = "select title name from auth_rule where id in ($rules) limit 4";

    		$data = M()->query($sql);

    		$rulesname= [];

    		foreach ($data as $rule) {
    			$rulesname[]=$rule['name'];
    		}

    		$groups[$k]['rulename']=implode(',',$rulesname);

    	}
    	$this->assign('groups',$groups);
        $this->display();
    }

    public function addrole($value='')
    {

    	$rules = M('auth_rule t1')->field("t1.*,t2.cname")->join("left join auth_cate t2 on t1.cid=t2.cid ")->select();

    	$tempArr = array();

    	foreach ($rules as  $row) {
    		$tempArr[$row['cname']][] = $row;
    	}

    	$this->assign('rules',$tempArr);

    	$this->display();
    }

    public function addroledata()
    {
    	$data = I('post.');

    	$data['rules'] = implode(',',$data['rules']);

    	$res = M('auth_group')->add($data);

    	if($res){
    		$this->ajaxReturn(['error'=>0,'info'=>"增加成功"]);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>"增加失败"]);
    	}
    }



}